package com.qf.smsplatform.controller;

import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.annotions.LogOperationAnno;
import com.qf.smsplatform.dto.R;
import com.qf.smsplatform.pojo.MyAuthUser;
import com.qf.smsplatform.pojo.TAdminUser;
import com.qf.smsplatform.pojo.TBlackList;
import com.qf.smsplatform.service.BlackListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/black")
@Api(tags = "黑名单管理")
/*2021/10/25*/public class BlackListController {

        private BlackListService blackListService;
        @Autowired
        public void setBlackListService(BlackListService blackListService) {
                this.blackListService = blackListService;
        }

        @GetMapping("/selectOne")
        @ApiOperation("查询单个黑名单数据")
        public R findBlackById(@ApiParam(name = "id",value = "黑名单的id") Long id){
                TBlackList blackById = blackListService.findBlackById(id);
                return R.setOk(blackById);
        }

        @GetMapping("/addBlack")
        @ApiOperation("添加黑名单数据信息")
        public R addBlack(String mobile, HttpSession session){

                //从登录用户中获取信息
                MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                TBlackList black=new TBlackList();
                if(user!=null){
                        black.setMobile(mobile);
                        black.setOwntype(user.getType());
                        black.setCreater(user.getId());
                }
                //调用service进行黑名单数据的插入
                blackListService.addBlack(black);
                return R.setOk();
        }

        @GetMapping("/selectPage")
        @ApiOperation("分页查询黑名单的数据信息")
        public R selectPage(@RequestParam(defaultValue = "1") int pageNum,@RequestParam(defaultValue = "5") int pageSize){
                PageInfo<TBlackList> pageInfo = blackListService.selectPage(pageNum, pageSize);
                return R.setOk(pageInfo);
        }

        @DeleteMapping("/delete")
        @ApiOperation("删除黑名单信息")
        public R deleteByIds(@RequestBody Map map){
                Integer integer = blackListService.deleteByIds((List<Integer>)map.get("ids"));
                if(integer>0){
                        return R.setOk();
                }
                return R.setError();
        }
        @PostMapping("/update")
        @ApiOperation("修改黑名单的信息")
        public R updateBlack(String mobile,Long id,HttpSession session){
                //从session中获取当前登录用户的信息
                MyAuthUser user = (MyAuthUser)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
                //先查询当前数据是由谁创建的
                TBlackList blackById = blackListService.findBlackById(id);
                //封装为对象，调用业务层进行数据的修改，相关判断交给业务层进行
                TBlackList black=new TBlackList();
                if(user!=null){
                        black.setMobile(mobile);
                        black.setOwntype(blackById.getOwntype());
                        black.setCreater(blackById.getCreater());
                        black.setId(id);
                }
                blackListService.updateBlack(black);
                return R.setOk();
        }
        @LogOperationAnno("查询黑名单和用户关系的数据")
        @GetMapping("/selectBA")
        @ApiOperation("查询黑名单和用户关系的数据")
        public R selectTBAndUser(){

                List<TBlackList> blackLists = blackListService.selectTBlackAndUser();
                return R.setOk(blackLists);
        }

        @GetMapping("/multParams")          //动态参数查询
        @ApiOperation("使用动态参数查询黑名单信息")
        public R selectByMultParams(@ApiParam(name = "tBlackList",value = "黑名单对象") TBlackList tBlackList) throws InstantiationException, IllegalAccessException {
                List<TBlackList> blackLists = blackListService.selectByMultParams(tBlackList);
                return R.setOk(blackLists);
        }


}
